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40 0x38 0x04 0x48 0x30  # S
	0x00 0x0C 0x12 0x22 0x24 0x44 0x48 0x30  # O
	0x00 0x0C 0x12 0x12 0x3E 0x28 0x44 0x42  # R
	0x00 0x1C 0x22 0x40 0x38 0x04 0x48 0x30  # S
	0x00 0x00 0x14 0x0E 0x5F 0x1F 0x2E 0x48
	0x00 0x00 0x00 0x00 0xFF 0xFF 0x00 0x00
	0x00 0x00 0x00 0x00 0xFF 0xFF 0x00 0x00
	0x00 0x00 0x00 0x00 0xFF 0xFF 0x00 0x00
	0x00 0x00 0x00 0x00 0xFF 0xFF 0x00 0x00
	0x00 0x00 0x00 0x00 0xFF 0xFF 0x00 0x00
	0x00 0x00 0x00 0x00 0xFE 0xFE 0x00 0x00
	0x00 0x00 0xF0 0x78 0xFE 0xFE 0x78 0xF0
	0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
	0x00 0x00 0x00 0xEE 0xAA 0xEC 0x8A 0x00  # PR
	0x00 0x00 0x00 0xEE 0xC8 0x86 0xEE 0x00  # ES
	0x00 0x00 0x00 0xE1 0x81 0x61 0xE1 0x00  # S
	0x00 0x00 0x00 0xDD 0x55 0xD5 0x54 0x00  # AN
	0x00 0x00 0x00 0x42 0x43 0xC3 0x82 0x00  # YK
	0x00 0x00 0x00 0xBA 0x32 0x23 0xB9 0x00  # E
	0x00 0x00 0x00 0x80 0x80 0x80 0x00 0x00  # Y

: intro_2_left
	0x00 0x00 0x00 0x09 0x09 0x0F 0x01 0x0F  # Y
	0x00 0x00 0x00 0x3C 0x24 0x24 0x24 0x3C  # O
	0x00 0x00 0x00 0x90 0x90 0x90 0x90 0xF0  # U
	0x00 0x00 0x00 0x12 0x12 0x12 0x0A 0x06  # V

: intro_2_right
	0x00 0x00 0x00 0x78 0x40 0x78 0x08 0x78  # S
	0x00 0x00 0x00 0x0F 0x08 0x08 0x08 0x0F  # C
	0x00 0x00 0x00 0x3C 0x24 0x3C 0x20 0x20  # P
	0x00 0x00 0x00 0x90 0x90 0x90 0x90 0xF0  # U

:alias KEY v3
:alias score_user v4
:alias score_cpu v5
:alias move_user v6
:alias move_cpu v7
:alias spr_x vA
:alias spr_y vB
:alias d_time vC
:alias tile_len vD

:const spr_x_user 13
:const spr_x_cpu 45
:const spr_y_common 18
:const tile_pix 8
:const time 70

:macro draw_tiles FX FY TX TY SPR {
	i := SPR
	spr_x := FX
	spr_y := FY
	tile_len := tile_pix
	
	loop
		sprite spr_x spr_y 8
		spr_x += 8
		i += tile_len
		if spr_x == TX then spr_y += 8
		if spr_x == TX then spr_x := FX
		if spr_y != TY then
	again
}

:macro draw_indctr X Y SPR {
	spr_x := X
	spr_y := Y
	i := SPR
	sprite spr_x spr_y 3
}

:macro draw_score X Y SCR {
	spr_x := X
	spr_y := Y
	i := digits
	bcd SCR
	load v2
	
	i := hex v0
	sprite spr_x spr_y 5
	spr_x += 5
	
	i := hex v1
	sprite spr_x spr_y 5
	spr_x += 5
	
	i := hex v2
	sprite spr_x spr_y 5
}

: main
	score_user := 0
	score_cpu := 0
	title_screen
	title_screen_2
	draw_score 9 9 score_user
	draw_score 41 9 score_user
	
	loop
		cycle
	again
;

: title_screen
	draw_tiles 0 0 64 32 background
	draw_tiles 0 0 64 32 intro_1
	KEY := key
	clear
;

: title_screen_2
	draw_tiles 0 0 32 32 background
	draw_tiles 0 8 32 16 intro_2_left
	draw_tiles 32 8 64 16 intro_2_right
	KEY := key
	draw_tiles 0 8 32 16 intro_2_left
	draw_tiles 32 8 64 16 intro_2_right
;

: cycle
	turn_user
	turn_cpu
	draw
	result
	draw
;

: turn_user
	# Exit loop if ASDZX is pressed
	loop
		move_user := 0
		KEY := key
		if KEY == OCTO_KEY_A then move_user := 1
		if KEY == OCTO_KEY_S then move_user := 2
		if KEY == OCTO_KEY_D then move_user := 3
		if KEY == OCTO_KEY_Z then move_user := 4
		if KEY == OCTO_KEY_X then move_user := 5
		if move_user == 0 then
	again
;

: turn_cpu
	vE := random 0xFF
	if vE >= 0x00 then move_cpu := 1
	if vE >= 0x32 then move_cpu := 2
	if vE >= 0x65 then move_cpu := 3
	if vE >= 0x98 then move_cpu := 4
	if vE >= 0xCB then move_cpu := 5
;

: draw
	spr_y := spr_y_common
	
	spr_x := spr_x_user
	if move_user == 1 then draw_rock
	if move_user == 2 then draw_paper
	if move_user == 3 then draw_scissors
	if move_user == 4 then draw_lizard
	if move_user == 5 then draw_spock
	
	spr_x := spr_x_cpu
	if move_cpu == 1 then draw_rock
	if move_cpu == 2 then draw_paper
	if move_cpu == 3 then draw_scissors
	if move_cpu == 4 then draw_lizard
	if move_cpu == 5 then draw_spock
;

: result
	if move_user == move_cpu then jump tie
	
	if move_user == 1 begin
		if move_cpu == 2 then jump won_cpu
		if move_cpu == 3 then jump won_user
		if move_cpu == 4 then jump won_user
		if move_cpu == 5 then jump won_cpu
	end
	
	if move_user == 2 begin
		if move_cpu == 1 then jump won_user
		if move_cpu == 3 then jump won_cpu
		if move_cpu == 4 then jump won_cpu
		if move_cpu == 5 then jump won_user
	end
	
	if move_user == 3 begin
		if move_cpu == 1 then jump won_cpu
		if move_cpu == 2 then jump won_user
		if move_cpu == 4 then jump won_user
		if move_cpu == 5 then jump won_cpu
	end
	
	if move_user == 4 begin
		if move_cpu == 1 then jump won_cpu
		if move_cpu == 2 then jump won_user
		if move_cpu == 3 then jump won_cpu
		if move_cpu == 5 then jump won_user
	end
	
	if move_user == 5 begin
		if move_cpu == 1 then jump won_user
		if move_cpu == 2 then jump won_cpu
		if move_cpu == 3 then jump won_user
		if move_cpu == 4 then jump won_cpu
	end
;

: tie
	draw_indctr 15 3 equals
	draw_indctr 47 3 equals
	dt
	draw_indctr 15 3 equals
	draw_indctr 47 3 equals
;

: won_user
	draw_indctr 15 3 plus
	draw_score 9 9 score_user
	score_user += 1
	draw_score 9 9 score_user
	dt
	draw_indctr 15 3 plus
;

: won_cpu
	draw_indctr 47 3 plus
	draw_score 41 9 score_cpu
	score_cpu += 1
	draw_score 41 9 score_cpu
	dt
	draw_indctr 47 3 plus
;

: dt
	d_time := time
	delay := d_time
	
	loop
		d_time := delay
		if d_time != 0 then
	again
;

: draw_rock
	i := rock
	sprite spr_x spr_y 7
;

: draw_paper
	i := paper
	sprite spr_x spr_y 7
;

: draw_scissors
	i := scissors
	sprite spr_x spr_y 7
;

: draw_lizard
	i := lizard
	sprite spr_x spr_y 7
;

: draw_spock
	i := spock
	spr_x -= 1  # spock's sprite length is 1 pixel extra
	sprite spr_x spr_y 7
;